package com.wpf.system.controller;

import cn.hutool.core.convert.Convert;
import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.ObjectUtil;
import com.alibaba.excel.EasyExcel;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.wpf.system.common.response.ResultUtils;
import com.wpf.system.common.response.ResultVO;
import com.wpf.system.common.utils.excelListener.EquipmentExcelListener;
import com.wpf.system.entity.Equipment;
import com.wpf.system.entity.vo.EquipmentVO;
import com.wpf.system.entity.vo.PageVO;
import com.wpf.system.service.EquipmentService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.List;

@RestController
@RequestMapping("/api/equipment")
public class EquipmentController {

    @Resource
    EquipmentService equipmentService;

    @GetMapping("/{id}")
    public Equipment getEquipmentById(@PathVariable("id") Long id){
        return equipmentService.getById(id);
    }

    @GetMapping
    public PageVO<EquipmentVO> getValidEquipmentList(
            @RequestParam(value = "page",required=false) Integer page,
            @RequestParam(value = "pageSize",required=false) Integer pageSize,
            @RequestParam(value = "cid",required=false) Long cid,
            @RequestParam(value = "searchContent",required=false) String searchContent ){
        List<EquipmentVO> equipmentVOList = equipmentService.getValidEquipment(cid,searchContent,null,null);
        int total = equipmentVOList.size();
        PageVO<EquipmentVO> equipmentListVO = new PageVO<>();
        equipmentListVO.setTotal(total);
        if(ObjectUtil.isNotNull(page)&&ObjectUtil.isNotNull(pageSize)){
            equipmentListVO.setList(equipmentService.getValidEquipment(cid,searchContent,Math.max((page - 1) * pageSize,0),pageSize));
        }else {
            equipmentListVO.setList(equipmentVOList);
        }
        return equipmentListVO;
    }

    @GetMapping("/list")
    public IPage<Equipment> getEquipmentList(
            @RequestParam("page") int page,
            @RequestParam("pageSize") int pageSize,
            @RequestParam(value = "cid",required=false) Long cid,
            @RequestParam(value = "searchContent",required=false) String searchContent ){
        return equipmentService.getEquipment(page,pageSize,cid,searchContent);
    }

    @PreAuthorize("hasAuthority('equipment:equipmentinfo:add')")
    @PostMapping
    public ResultVO addEquipment(@RequestBody Equipment equipment){
        equipment.setEquipmentNo("equipment"+IdUtil.objectId());
        if (equipmentService.save(equipment)){
            return ResultUtils.success("添加器材成功！",equipment.getEquipmentNo());
        }else{
            return ResultUtils.error("添加器材失败！");
        }
    }

    @PreAuthorize("hasAuthority('equipment:equipmentinfo:import')")
    @PostMapping("/batch/import")
    public ResultVO addUserBatch(@RequestParam("file") MultipartFile file) {
        EquipmentExcelListener listener = new EquipmentExcelListener(equipmentService);
        try {
            EasyExcel.read(file.getInputStream(), Equipment.class, listener ).sheet(0).headRowNumber(1).doRead();
        } catch (IOException e) {
            e.printStackTrace();
        }
        if(listener.getCount()>0){
            return ResultUtils.success("导入器材信息成功！共导入"+listener.getCount()+"条");
        }else{
            return ResultUtils.error("导入器材信息失败！请检查EXCEL表格的数据是否有误！");
        }
    }

    @PreAuthorize("hasAuthority('equipment:equipmentinfo:edit')")
    @PutMapping
    public ResultVO editEquipment(@RequestBody Equipment equipment){
        if (equipmentService.updateById(equipment)){
            return ResultUtils.success("修改器材信息成功！");
        }else{
            return ResultUtils.error("修改器材信息失败！");
        }
    }

    @PreAuthorize("hasAuthority('equipment:equipmentinfo:edit')")
    @PutMapping("/status")
    public ResultVO editEquipmentStatus(@RequestBody Long id){
        if (Convert.toBool(equipmentService.editEquipmentStatusById(id))){
            return ResultUtils.success("修改器材状态成功！");
        }else{
            return ResultUtils.error("修改器材状态失败！");
        }
    }

    @PreAuthorize("hasAuthority('equipment:equipmentinfo:delete')")
    @DeleteMapping("/{id}")
    public ResultVO deleteEquipment(@PathVariable("id") Long id){
        if (Convert.toBool(equipmentService.removeById(id))){
            return ResultUtils.success("删除器材成功！");
        }else{
            return ResultUtils.error("删除器材失败！");
        }
    }

    @PreAuthorize("hasAuthority('equipment:equipmentinfo:deletebatch')")
    @DeleteMapping("/batch/delete/{ids}")
    public ResultVO deleteEquipmentBatch(@PathVariable("ids") String ids){
        List<Long> deleteIds = Convert.toList(Long.class,ids.split(","));
        if(equipmentService.removeBatchByIds(deleteIds)){
            return ResultUtils.success("批量删除器材成功！");
        }
        return ResultUtils.error("批量删除器材失败！");
    }
}
